package org.TCC_NOVO.client.visual;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import java.util.Date;
import org.TCC_NOVO.client.GWTServiceAsync;

public class TarefasPanelAltStatus extends Window{
    private final GWTServiceAsync controller;
    private final String IDF_usuario;
    int ID_Tarefa;
    AssumirTarefasPanel TarefasPanel;

    Panel panelPrin;
    FormPanel formPanel;
    ComboBox cb;
    Record record;

    public TarefasPanelAltStatus(GWTServiceAsync controller, AssumirTarefasPanel TarPanel, int ID_Tarefa, String IDF_Usuario) {
        this.controller = controller;
        this.IDF_usuario = IDF_Usuario;
        this.TarefasPanel = TarPanel;
        this.ID_Tarefa = ID_Tarefa;
        this.init();
        this.setModal(true);
    }

    private void init(){
        panelPrin = getPanelCentral();
        this.add(panelPrin);
        this.setTitle("Alterar Status Tarefa");
    }

    private Panel getPanelCentral(){
        Panel panel = new Panel();
        panel.setBorder(false);
        panel.setPaddings(1);

        formPanel = new FormPanel();
        formPanel.setFrame(true);
        formPanel.setWidth(350);
        formPanel.setLabelWidth(75);

        final Store store = new SimpleStore(new String[]{"ID", "status"}, getStatus());
        store.load();

        cb = new ComboBox();
        cb.setForceSelection(true);
        cb.setMinChars(1);
        cb.setFieldLabel("Status");
        cb.setStore(store);
        cb.setDisplayField("status");
        cb.setValueField("ID");
        cb.setMode(ComboBox.LOCAL);
        cb.setTriggerAction(ComboBox.ALL);
        cb.setEmptyText("Selecione o Status");
        cb.setLoadingText("Searching...");
        cb.setTypeAhead(true);
        cb.setSelectOnFocus(true);
        cb.setWidth(200);
        cb.setHideTrigger(false);
        cb.setName("Status");
        formPanel.add(cb);

        final AsyncCallback callbackTarefasStatus = new AsyncCallback() {
            public void onSuccess(Object result) {}
            public void onFailure(Throwable caught) {
               MessageBox.alert("Erro ", caught.getMessage()); }
        };

        final AsyncCallback callbackTarefasOcor = new AsyncCallback() {
            public void onSuccess(Object result) {}
            public void onFailure(Throwable caught) {
               MessageBox.alert("Erro ", caught.getMessage()); }
        };

        final Button salvar = new Button();
        salvar.setText(" Salvar");
        salvar.setIcon("Images/salvar.png");
        salvar.setTooltip("Salvar");
        salvar.addListener(new ButtonListenerAdapter(){
            public void onClick(Button button, EventObject e){
                if (!cb.getValueAsString().equals("")){
                    //altera o status da tarefa
                    String sSql = "Update Atividade_Roteiro set Status = '"+cb.getRawValue().trim()+"' where ID = "+ID_Tarefa;
                    controller.EditarRegistro(sSql, callbackTarefasStatus);
                    formPanel.getForm().updateRecord(record);
                   
                    //Incluir ocorrencia de tarefa
                    DateTimeFormat sdf = DateTimeFormat.getFormat("yyyy-MM-dd");
                    String DataOco = sdf.format(new Date());

                    sSql = "insert into Ocorrencia_Tarefa (IDF_Tarefa, IDF_Usuario, Descricao, Data) values "+
                            "("+ID_Tarefa+", "+IDF_usuario+",'Status alterado para "+cb.getRawValue().trim()+"', '"+DataOco+"')";
                    controller.IncluirRegistro(sSql, callbackTarefasOcor);

                    //filtra o grid das tarefas
                    TarefasPanel.CarrgarGridMaster();
                    
                    hide();
                }else{
                    MessageBox.alert("Necessário informar um status!");
                }
            }
        });
        formPanel.addButton(salvar);

        panel.add(formPanel);
        return panel;
    }

    public void loadRecord(Record record){
        this.record = record;
        formPanel.getForm().loadRecord(record);
    }

    private static String[][] getStatus() {
        return new String[][]{
                new String[]{"1", "Aberta"},
                new String[]{"2", "Em Andamento"},
                new String[]{"3", "Pronta para Revisão"},
                new String[]{"4", "Com Problemas"},
                new String[]{"5", "Concluída"},
        };
    }

}
